package com.example.demo.service.impl;
import com.example.demo.LogUtil;
import com.example.demo.repository.AutoMessageHandler;
import com.example.demo.service.SpyderCOVID;
import com.example.demo.service.SpyderInterface;
import com.example.demo.service.SpyderInterfaceLight;
import com.normalClasses.eye.*;

import java.util.List;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.normalClasses.eye.socket_res_obj;

@Slf4j
@Service("test_spyder")
public class SpyderTestService implements SpyderInterface, SpyderInterfaceLight, SpyderCOVID {

	@Override
	
	public boolean check_over_csdn(String child_link) {
		List user=(List)jdbcTemplate.queryForList("select 1 from t_csdn_contents where `child_link` ='"+child_link+"'");
	    if(user.size()==0)return true;
	    else return false;

	}
	@Override
	public boolean check_over_csdn_back(String child_link,String parent_keyword) {
		// 查重,true为未重复


		List user=(List)jdbcTemplate.queryForList("select 1 from t_csdn_back where `child_link` ='"+child_link+"' and `parent_keyword` = '"+parent_keyword+"'");
	    if(user.size()==0)return true;
	    else return false;

	}
@Override
public boolean check_over_runoob(String keyword) {
	// 查重,true为未重复

	
	List user=(List)jdbcTemplate.queryForList("SELECT 1 FROM `t_runoob_contents` WHERE keyword = ?",keyword);
    if(user.size()==0)return true;
    else return false;
//		Map people=(Map)user.get(0);
//	    String nickname=(String)people.get("nickname");

}

public static String getURLEncoderString(String str) {
    String result = "";
    if (null == str) {
        return "";
    }
    try {
        result = java.net.URLEncoder.encode(str, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return result;
}
public static String URLDecoderString(String str) {
    String result = "";
    if (null == str) {
        return "";
    }
    try {
        result = java.net.URLDecoder.decode(str, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return result;
}
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void get_test(String url) throws IOException {
    //从URL加载HTML
    Document document = Jsoup.connect(url).get();
    String title = document.title();
    Element e = document.getElementById("head_wrapper");
    //获取html中的标题
    //System.out.println();
    log.info(LogUtil.logSpec("text :"+document.html(), 5));
    //从字符串加载HTML
    String html = "<html><head><title>First parse</title></head>"
            + "<body><p>Parsed HTML into a doc.</p></body></html>";
    Document doc = Jsoup.parse(html);
    title = doc.title();
    //System.out.println("title :"+title);

    //从文件加载HTML
//    doc = Jsoup.parse(new File("F:\\jsoup\\html\\index.html"),"utf-8");
//    title = doc.title();
//    System.out.println("title :"+title);
}

public static String[] my_append(String[] a,String next)
{
	
	int alength =a.length;

	
	String[] tempArray=new String[a.length+1];
	for(int i=0;i<a.length;i++)
	{
		tempArray[i]=a[i];
	}
	tempArray[a.length]=next;
	return tempArray;
}
public int[] StringToInt(String[] arrs){//用于时间json那里
	 int[] ints = new int[arrs.length];
	    for(int i=0;i<arrs.length;i++){

	    	if(i==0)ints[i] = Integer.parseInt(String.valueOf(arrs[i].charAt(1)));
	    	else if(i==arrs.length-1)ints[i] = Integer.parseInt(String.valueOf(arrs[i].charAt(0)));
	    	else ints[i] = Integer.parseInt(arrs[i]);

	    }
	return ints;
	 }
public  String delHtmlTags (String htmlStr) {
    //定义script的正则表达式，去除js可以防止注入
    String scriptRegex="<script[^>]*?>[\\s\\S]*?<\\/script>";
    //定义style的正则表达式，去除style样式，防止css代码过多时只截取到css样式代码
    String styleRegex="<style[^>]*?>[\\s\\S]*?<\\/style>";
    //定义HTML标签的正则表达式，去除标签，只提取文字内容
    String htmlRegex="<[^>]+>";
    //定义空格,回车,换行符,制表符
    String spaceRegex = "\\s*|\t|\r|\n";

    // 过滤script标签
    htmlStr = htmlStr.replaceAll(scriptRegex, "");
    // 过滤style标签
    htmlStr = htmlStr.replaceAll(styleRegex, "");
    // 过滤html标签
    htmlStr = htmlStr.replaceAll(htmlRegex, "");
    // 过滤空格等
    htmlStr = htmlStr.replaceAll(spaceRegex, "");
    return htmlStr.trim(); // 返回文本字符串
}
@Override
public void spy_CSDN_back(String key,String parent_key,String p)  throws IOException{
	String url="https://so.csdn.net/so/search/s.do?q="+getURLEncoderString(key)+"&t=blog&o=&p="+p;//p=1,p=2可以翻页
	//&o=new,搜索最新，o不传参搜索最热。
	//&t=ask，搜索问答，blog搜索博客
	System.out.print("URL:"+url+"\n");
	Document document = Jsoup.connect(url).get();
    Elements aaa=document.getElementsByClass("search-list J_search");
    for(Element small_ele:aaa) {
    	Element link = small_ele.selectFirst("a");
    	String relHref = link.attr("href");
    	//System.out.print(relHref);
    	String title_abstract=delHtmlTags(link.html());
    	String content_abstract = delHtmlTags(small_ele.getElementsByTag("dd").text());
    	if(check_over_csdn(relHref)) {
    		if(check_over_csdn_back(relHref,parent_key)) {
    			System.out.print(relHref+"\n"+key+"\n"+title_abstract+"----------------\n");
    			jdbcTemplate.update("INSERT INTO `t_csdn_back`(`child_keyword`, `parent_keyword`, `tittle_abstract`, `content_abstract`, `child_link`, `value`) VALUES (?,?,?,?,?,?) ",key,parent_key,title_abstract,content_abstract,relHref,"5");
    		}
    	}
    }
}
@Override
public void spy_CSDN(String key,String cookie,String p)  throws IOException{
	String url="https://so.csdn.net/so/search/s.do?q="+getURLEncoderString(key)+"&t=blog&o=&p="+p;//p=1,p=2可以翻页
	//&o=new,搜索最新，o不传参搜索最热。
	//&t=ask，搜索问答，blog搜索博客
	System.out.print("URL:"+url+"\n");
	Document document = Jsoup.connect(url).get();
    String title = document.title();
    Elements links = document.select("a[href]");
    Elements aaa=document.getElementsByClass("search-list J_search");
    Elements aaa_links=document.select("a");
    for(Element small_ele:aaa) {
    	Element link = small_ele.selectFirst("a");
    	String relHref = link.attr("href");
    	System.out.print(relHref);
    	String title_abstract=delHtmlTags(link.html());
    	String content_abstract = delHtmlTags(small_ele.getElementsByTag("dd").text());
    	String date = small_ele.getElementsByClass("author-time").last().getElementsByClass("date").html();
        Map<String, String> header = new HashMap<String, String>();
        header.put("Cookie", cookie);
        try {
        	Document child_doc = Jsoup.connect(relHref).headers(header).get();
	        	       // Document child_doc = connect.get();
	    	//Elements questions_detail_con = child_doc.getElementsByClass("questions_detail_con");
	    	Elements child_body = child_doc.getElementsByClass("article_content clearfix");
	
	    	String content = delHtmlTags(child_body.first().getElementById("content_views").html());
	    	
	    	//System.out.print();
	    	title = child_doc.title();
	    	if(check_over_csdn(relHref))
	    	jdbcTemplate.update("INSERT INTO `t_csdn_contents`(`keyword`, `tittle_abstract`, `content_abstract`, `tittle`, `content`, `child_link`, `date`, `value`) VALUES (?,?,?,?,?,?,?,?) ",key,title_abstract,content_abstract,title,content,relHref,date,"5");
	    	//jdbcTemplate.update("INSERT INTO `t_csdn_contents`(`keyword`) VALUES (?)",key);
	
	    	//List user=(List)jdbcTemplate.queryForList("SELECT * FROM `t_csdn_contents` ");
	    	System.out.print(key+"\n"+title_abstract+"\n"+content_abstract+"\n"+title+"\n"+content+"\n"+relHref+"\n"+date);
	    	System.out.print("\n--------------\n");
        }
    	catch(HttpStatusException e) {
    		System.out.print("这个子链接404");
    	}
    }
}


	@Override
	public void get_jianshu(String key,String cookie,String p) throws IOException{
		//限于进度，暂时搁置
		String url="https://www.jianshu.com/search?q="+key+"&page=1&type=note";
        Map<String, String> header = new HashMap<String, String>();
        header.put("Accept","application/json");
        header.put("Referer",url);
        header.put("Cookie", cookie);
        
		Document document = Jsoup.connect(url).headers(header).post();
	    String title = document.title();
	    Element body = document.body();
	    //JSONObject json = JSONObject.fromObject(body.text());
	    //System.out.print(body.html());
	}
	
	@Override
	public void  get_runoob() throws IOException{
		String url = "https://www.runoob.com/cprogramming/c-tutorial.html";
		Document document = Jsoup.connect(url).get();
		
		Element left = document.getElementsByClass("design").first();
		Iterator it = left.getElementsByTag("a").iterator();
        
	    while(it.hasNext()){
	        //System.out.println("link : "+ link.attr("href"));
	    	Element element = (Element)it.next();
	    	String keyword = element.text();
	        //System.out.println("题目名字 :"+ element.text());
	        
	        if(!check_over_runoob(keyword)) continue;
	        //System.out.println("\n子链接:"+"https://www.runoob.com"+element.attr("href"));
	        String child_link = "https://www.runoob.com"+element.attr("href");
	        
	        
	        Document child_doc = Jsoup.connect(child_link).get();
	        Element child_text = child_doc.getElementById("content");
	        Element content = child_text.getElementsByClass("article-intro").first();
	        

			int index_comments=0;
			int index_comment_code=0;
			int index_main_code=0;
			//主内容+主代码
			//System.out.println("主界面内容："+delHtmlTags(content.html()));
			jdbcTemplate.update("INSERT INTO `t_runoob_contents`(`keyword`, `main_text_content`) VALUES (?,?)",keyword,delHtmlTags(content.html()));
			
			try {
				Elements test2=content.getElementsByClass("example_code");
	        	Elements main_codes = content.getElementsByClass("example_code");
		        Iterator main_code_it = main_codes.iterator();
				
				while(main_code_it.hasNext()) {//主区域代码段
					Element main_code=(Element)main_code_it.next();
					//System.out.print("main_code:  "+main_code.html());
					jdbcTemplate.update("INSERT INTO `t_runoob_codes`(`keyword`, `code_belong`, `code_index`, `code_content`) VALUES (?,?,?,?)",keyword,"main",String.valueOf(index_main_code),main_code.html());
					index_main_code+=1;
				}
				 log.info(LogUtil.logSpec("Get rookie content successfully", 5));
			}
			
			catch(NullPointerException e) {
	        	log.info(LogUtil.logSpec(child_link+",There is no main code segment for this connection", 5));
			}

			//评论区域
	        Element comment = child_doc.getElementsByClass("commentlist").first();
	        try {
	        	Elements test =comment.getElementsByClass("comt-main");
	        	Elements comments = comment.getElementsByClass("comt-main");
		        Iterator com_it = comments.iterator();
				
				
				while(com_it.hasNext()) {//循环评论区
					
					Element child_element = (Element)com_it.next();
					try {
						Elements test1=child_element.getElementsByTag("pre");
			        	Elements code = child_element.getElementsByTag("pre");
				        Iterator code_it = code.iterator();
				
						while(code_it.hasNext()) {//评论区的代码段
							Element child_code = (Element)code_it.next();
							//System.out.println("comment_code:  "+child_code.html());
							jdbcTemplate.update("INSERT INTO `t_runoob_codes`(`keyword`, `code_belong`, `code_index`, `code_content`) VALUES (?,?,?,?)",keyword,"comment",String.valueOf(index_comments),child_code.html());
							index_comment_code+=1;
						}
					}
					catch(NullPointerException e) {
						log.info(LogUtil.logSpec(child_link+",There is no main code segment for this connection", 5));
					}

					child_element.getElementsByTag("pre").remove();
					//System.out.println("评论区内容： "+delHtmlTags(child_element.html()));
					//System.out.print("-------------------------\n");
					
					jdbcTemplate.update("INSERT INTO `t_runoob_comments`(`keyword`, `comment_index`, `comment_content`) VALUES (?,?,?)",keyword,String.valueOf(index_comments),delHtmlTags(child_element.html()));
					index_comments+=1;
				}
	        }
	        catch(NullPointerException e) {
	        	
	        	Elements test=null;
				log.info(LogUtil.logSpec(child_link+",There is no main code segment for this connection", 5));
	        }
	
	        //break;
	    }
	    
	}

	@Override
	public	JSONObject search_runoob(String keyword) {
		JSONObject ans = new JSONObject();
		JSONObject comments = new JSONObject();
		JSONObject content = new JSONObject();
		ans.put("keyword",keyword);
		
		List t_runoob_codes=(List)jdbcTemplate.queryForList("SELECT * FROM `t_runoob_codes` WHERE keyword = '"+keyword+"'");
		List t_runoob_contents=(List)jdbcTemplate.queryForList("SELECT * FROM `t_runoob_contents` WHERE keyword = '"+keyword+"'");
		int size1;
		
		if((size1 = t_runoob_contents.size())>0) {
			Map content_map=(Map)t_runoob_contents.get(0);
			content.put("text",content_map.get("main_text_content").toString());
		}
		List t_runoob_comments=(List)jdbcTemplate.queryForList("SELECT * FROM `t_runoob_comments` WHERE keyword = '"+keyword+"'");
		int size2;
		int listsize2;
		int listsize3;
		int ii;
		if((size2 = t_runoob_comments.size())>0) {
			for(ii=0;ii<size2;ii++) {
				JSONObject comment = new JSONObject();
				Map content_map=(Map)t_runoob_comments.get(ii);
				comment.put("text",content_map.get("comment_content").toString());
				if((listsize3=t_runoob_codes.size())>0) {
					for(int iii=0;iii<listsize3;iii++) {
						Map code_map_1=(Map)t_runoob_codes.get(iii);
						if(code_map_1.get("code_belong").toString().equals("comment")) {
							comment.put("code_"+code_map_1.get("code_index").toString(), code_map_1.get("code_content").toString());
						}
					}
				}
				comments.put("comment_"+content_map.get("comment_index").toString(),comment);
				
			}
			//System.out.print(comments.toString());
		}
		
		
		
		int listsize;
		if((listsize=t_runoob_codes.size())>0){
			//初始化一个map
			
			//Map<String,String> maplist=new HashMap<String, String>();
			for(int i=0;i<listsize;i++){
				Map code_map=(Map)t_runoob_codes.get(i);

				if(code_map.get("code_belong").toString().equals("main")) {
					content.put("code_"+code_map.get("code_index").toString(), code_map.get("code_content").toString());
				}
			}
		//return maplist;
		}
		ans.put("content",content);
		ans.put("comments",comments);
		 log.info(LogUtil.logSpec("Get rookie successfully"+keyword+"content", 5));

		//System.out.print(ans.toString());
		//System.out.print("\n-------------------------\n");

		
		return ans;

		
	}
	@Override
	public JSONObject get_runoob_comments(String keyword) {
		JSONObject comments = new JSONObject();
		List t_runoob_codes=(List)jdbcTemplate.queryForList("SELECT * FROM `t_runoob_codes` WHERE keyword = '"+keyword+"'");
		List t_runoob_comments=(List)jdbcTemplate.queryForList("SELECT * FROM `t_runoob_comments` WHERE keyword = '"+keyword+"'");
		int size1;
		int size2;
		JSONArray arr_comments = new JSONArray();
		
		int listsize2;
		int listsize3;
		int ii;
		if((size2 = t_runoob_comments.size())>0) {
			for(ii=0;ii<size2;ii++) {
				JSONObject comment = new JSONObject();
				
				Map content_map=(Map)t_runoob_comments.get(ii);
				comment.put("text",content_map.get("comment_content").toString());
				if((listsize3=t_runoob_codes.size())>0) {
					String arr_comment_code[] = {};
					for(int iii=0;iii<listsize3;iii++) {
						Map code_map_1=(Map)t_runoob_codes.get(iii);
						if(code_map_1.get("code_belong").toString().equals("comment")) {
							arr_comment_code=my_append(arr_comment_code,code_map_1.get("code_content").toString());
							
						}
					}
					
					comment.put("code", arr_comment_code);
				}
				//comments.put("comment_"+content_map.get("comment_index").toString(),comment);
				//System.out.print(comment.toString());
				arr_comments.add(comment);
				//return comment;
			}
			
			comments.put("comment",arr_comments);
			return comments;
		}
		
		comments.put("comment",arr_comments);
		return comments;
	}
	
	@Override
	public JSONObject get_runoob_contents(String keyword) {
		JSONObject content = new JSONObject();
		int listsize;
		List t_runoob_contents=(List)jdbcTemplate.queryForList("SELECT * FROM `t_runoob_contents` WHERE keyword ='"+keyword+"'");
		List t_runoob_codes=(List)jdbcTemplate.queryForList("SELECT * FROM `t_runoob_codes` WHERE keyword ='"+keyword+"'");
		int size1;
		
		if((size1 = t_runoob_contents.size())>0) {
			Map content_map=(Map)t_runoob_contents.get(0);
			content.put("text",content_map.get("main_text_content").toString());
			//System.out.print("0"+content);

		}	
		if((listsize=t_runoob_codes.size())>0){
			String arr_content_code[] = {};

			for(int i=0;i<listsize;i++){
				Map code_map=(Map)t_runoob_codes.get(i);
				if(code_map.get("code_belong").toString().equals("main")) {
					arr_content_code=my_append(arr_content_code,code_map.get("code_content").toString());
				
				}
			}
			//System.out.print("2"+content);
			if(arr_content_code.length>0)
			content.put("code", arr_content_code);
		//System.out.print("3"+content);
		return content;
		}
		String[] kong = {};
		content.put("code",kong );
		return content;
	}
	
	@Override
	public JSONObject get_menu(long stamp) {
		JSONObject menu = new JSONObject();
		menu.put("response_index",0);
		menu.put("req_time_stamp",stamp);
		List t_runoob_contents=(List)jdbcTemplate.queryForList("SELECT keyword FROM `t_runoob_contents` ");
		int size1;
		int i;
		double jjjjj;
		String[] cat= {};
		
		String[] my_menu  = get_keyword();
	    String keyword;
	    JSONObject con;
	    Key_word_similar aaaa;
		if((size1 = t_runoob_contents.size())>0) {
			JSONArray nodes = new JSONArray();
			
			for(i=0;i<size1;i++) {
				Map content_map=(Map)t_runoob_contents.get(i);
				JSONObject node = new JSONObject();
				node.put("name",content_map.get("keyword"));
				node.put("value",i);
				jjjjj = Math.random();
				node.put("symbolSize",100+(int)(jjjjj*5)*20);
				node.put("category",i/10);
				nodes.add(node);
				if(i%10 == 0)cat = my_append(cat,String.valueOf(i/10));
			}
			menu.put("nodes",nodes);
			
			//content.put("text",content_map.get("main_text_content").toString());
			//edges我现在准备瞎几把生成。
			//JSONObject edges = new JSONObject();
			int j;
			int ii,jj;
			JSONArray edges_arr = new JSONArray();
			for(i=0;i<5;i++) {
				for(j=0;j<5;j++) {
					JSONObject edge = new JSONObject();
					ii=(int)(51*Math.random());
					jj=(int)(51*Math.random());
					while(ii==jj)jj=(int)(51*Math.random());
					edge.put("source",ii);
					//System.out.print("1111");
					edge.put("target",jj);
			    	keyword = my_menu[ii];
			    	con = get_runoob_contents(keyword);
			    	String cmp_a = con.getString("text");
			    	keyword = my_menu[jj];
			    	con = get_runoob_contents(keyword);
			    	String cmp_b = con.getString("text");
			    	aaaa = new Key_word_similar(cmp_a,cmp_b);
					edge.put("value",100+(int)(100*aaaa.getRes_data()));
					edges_arr.add(edge);
				}
			}
			
			//edges.put("edges",edges_arr);
			menu.put("edges",edges_arr);
			int spans;
			int[] cat_int= new int[cat.length];
			for(spans=0;spans<cat.length;spans++)cat_int[spans]=Integer.valueOf(cat[spans]);
			menu.put("categories",cat);
			log.info(LogUtil.logSpec("Get menu file successfully", 5));
			return menu;
		}
		return null;
	}
	
	
	
	@Override
	public String[] get_keyword() {
		List t_runoob_contents=(List)jdbcTemplate.queryForList("SELECT keyword FROM `t_runoob_contents` ");
		int size;
		
		if((size=t_runoob_contents.size())>0) {
			int i;
			String menu[]= new String[size];
			//JSONObject new_menu= new JSONObject;
			//System.out.print(menu);
			for(i=0;i<size;i++) {
				Map content_map=(Map)t_runoob_contents.get(i);
				menu[i]=content_map.get("keyword").toString();
				//menu=my_append(menu,content_map.get("keyword").toString());

			}
			return menu;
		}
		return null;
	}

	@Override
	public JSONObject get_from_CSDN(int index,String keyword ,long req_time_stamp) {
		List t_csdn_contents = (List)jdbcTemplate.queryForList(("select `tittle` ,`content_abstract`, `content` from `t_csdn_contents` where keyword = '"+keyword+"'"));
		int size;
		size=t_csdn_contents.size();
		JSONObject ans=new JSONObject();
		
		if(index>size+2) {
			ans.put("req_time_stamp",0);
			return ans;
		}
		if(size>0) {
			int i;
			Map content_map=(Map)t_csdn_contents.get(index+2);
			ans.put("req_time_stamp",req_time_stamp);
			ans.put("title",content_map.get("tittle").toString());
			ans.put("abstract",content_map.get("content_abstract").toString());
			ans.put("text",content_map.get("content").toString());
			return ans;
		}
		return null;
	}
	@Override
	public void spy_csdn_by_runoobs_menu(String cookie) throws IOException {
		String menu[] =  get_keyword();
		int l = menu.length;
		int i;
		for(i=36;i<l;i++) {
			spy_CSDN(menu[i],cookie,"1");
		}
	}
	@Override
	public String man_evaluate_csdn(int index,String keyword_index,String opt,String value) {
		//System.out.print("SELECT 1 FROM `t_csdn_contents` where keyword =' "+keyword+"'");
		String menu[] = get_keyword();
		String keyword = menu[Integer.valueOf(keyword_index)];
		List csdn = (List)jdbcTemplate.queryForList("SELECT 1 FROM `t_csdn_contents` where keyword ='"+keyword+"'");
		int size;
		if((size=csdn.size())>0) {
			List csdn_con = (List)jdbcTemplate.queryForList("SELECT `tittle`,`content`,`date`,`child_link`,`value` FROM `t_csdn_contents` where keyword ='"+keyword+"'");
			if(csdn_con.size()<index)return "超索引了";
			Map csdn_map =(Map)csdn_con.get(index);
			 switch (opt) {
			 	 case "get":
			 		 return "<!DOCTYPE html><html><h1>"+csdn_map.get("tittle").toString()+" 日期:"+csdn_map.get("date").toString()+" 原始的评分："+csdn_map.get("value").toString()+"</h1><br>"+"<h3>"+csdn_map.get("content").toString()+"</h3></html>";
			 	 case "put":
			 		jdbcTemplate.update("UPDATE `t_csdn_contents` SET `value`= '"+value+"'WHERE child_link='"+csdn_map.get("child_link").toString()+"'");
			 		return csdn_map.get("tittle").toString()+ "更新成功";
			 }
		}
		else return "没有这个屌keyword";	
		return null;
	}
	
	@Override
	public void periodical_delate_low_csdn() {
		
	}
	
	@Override
	public String get_back_csdn(String parent_key) {
		List csdn = (List)jdbcTemplate.queryForList("SELECT `tittle_abstract`,`content_abstract`,`child_link` FROM `t_csdn_back` where parent_keyword ='"+parent_key+"'");
		int size;
		if((size=csdn.size())>0) {
			int index = (int)(Math.random()*size);
			Map con = (Map)csdn.get(index);
			message message=new message();
			message.setTitle(con.get("tittle_abstract").toString());
			message.setContent(con.get("content_abstract").toString()+"\n这篇文章的链接是： "+con.get("child_link").toString());
			String msg=JSON.toJSONString(message);
			//System.out.print(msg);
			log.info(LogUtil.logSpec("Get CSDN message successfully", 5));
			return msg;
		}
		else return "nope";
	}
	@Override
	public String get_back_csdn_cos(String parent_key) {
		List csdn = (List)jdbcTemplate.queryForList("SELECT `tittle_abstract`,`content_abstract`,`child_link` FROM `t_csdn_back` where parent_keyword ='"+parent_key+"'");
		int size;
		if((size=csdn.size())>0) {
			int index = (int)(Math.random()*size);
			Map con = (Map)csdn.get(index);
			message message=new message();
			message.setTitle(con.get("tittle_abstract").toString());
			message.setContent(con.get("content_abstract").toString()+"\n这篇文章的链接是： "+con.get("child_link").toString());
			String msg=JSON.toJSONString(message);
			log.info(LogUtil.logSpec("Get CSDN backup message succeeded", 5));
			return msg;
		}
		else return "nope";
		
	}
	@Override
	public void storage_userhabit(String source_id,int event_id,String username) throws ParseException {
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
		SimpleDateFormat sf1 = new SimpleDateFormat("HH");

		String time_str = sf.format(date);
		String hour_str = sf1.format(date);
		List user = (List)jdbcTemplate.queryForList("SELECT `json` FROM `t_user_habit` where username ='"+username+"'");
		Map user_map = null;
		JSONObject user_fin;
		if(user.size() == 0 ) {
			//这个用户之前没有存储过的情况，插入一列
			JSONObject ans = new JSONObject();
			int[] total_times = new int[52];//高危，为了目前的方便，暂用数字代替了。。。不然传参相当麻烦，计算又费时间。
			int[] total_duration = new int[52];
			int[] accumulate_habit = new int[24];//24小时
			int distracted_times = 0;//走神次数
			String last_start_time = "NaN";
			ans.put("total_times",total_times);
			ans.put("total_duration",total_duration);
			ans.put("accumulate_habit",accumulate_habit);
			ans.put("distracted_times",distracted_times);
			ans.put("last_start_time",last_start_time);//上次开始时间，用于下次计算时常
			jdbcTemplate.update("INSERT INTO `t_user_habit`(`username`, `json`) VALUES (?,?)",username,ans.toString());//转字符串更新
			user_fin = ans;
		}
		else {
			user_map = (Map)user.get(0);
			//System.out.print(user_map.get("json"));
			//System.out.print("\n");
			user_fin = (JSONObject) JSONObject.parse((String) user_map.get("json")) ;
		}
		switch(event_id) {
		case 4://开始学习,更新次数和上次开始时间
			//System.out.print(StringToInt(user_fin.get("accumulate_habit").toString().split(",")));
			int[] total_1 =StringToInt(user_fin.get("accumulate_habit").toString().split(","));
			total_1[Integer.valueOf(hour_str)]+=1;//更新时段频率
			user_fin.put("accumulate_habit",total_1);
			int[] total_3 = StringToInt(user_fin.get("total_times").toString().split(","));
			total_3[Integer.valueOf(source_id)]+=1;//更新source_id
			user_fin.put("total_times",total_3);
			user_fin.put("last_start_time",time_str);//用分时表。更新last_start
			jdbcTemplate.update("UPDATE `t_user_habit` SET `json`=? WHERE `username` = ?",user_fin.toString(),username);
			break;
			
		case 5://结束学习，更新总时长和上次开始时间
			int[] total_2 = StringToInt(user_fin.get("total_duration").toString().split(","));
			String source_time = (String) user_fin.get("last_start_time");
			
			user_fin.put("total_duration",total_2);
			if(!source_time.equals("NaN")) {
				Date d1 = (Date) sf.parse(source_time);
				Date d2 = (Date) sf.parse(time_str);
				total_2[Integer.valueOf(source_id)]+=(((d2.getTime() - d1.getTime()) / 1000)/60);
			}

			user_fin.put("last_start_time","NaN");
			jdbcTemplate.update("UPDATE `t_user_habit` SET `json`=? WHERE `username` = ?",user_fin.toString(),username);
			break;			
			
		}	
	}
	
	@Override
	public String get_user_notification(String username){
		List user = (List)jdbcTemplate.queryForList("SELECT `json` FROM `t_user_habit` where username ='"+username+"'");
		Map user_map = (Map)user.get(0);
		JSONObject user_fin = (JSONObject) JSONObject.parse((String) user_map.get("json"));
		int[] total_1 =StringToInt(user_fin.get("accumulate_habit").toString().split(","));
		int[] total_2 = StringToInt(user_fin.get("total_duration").toString().split(","));
		int[] total_3 = StringToInt(user_fin.get("total_times").toString().split(","));
		int i,j;
		int max = 0;
		int max_accumulate_habit = 0;
		for(i=0;i<total_1.length;i++) {//喜欢的学习时间
			if(total_1[i]>=max) {
				max = total_1[i];
				max_accumulate_habit = i;
			}
		}

		max = 0;
		int max_times_index = 9999;
		for(i=0;i<total_3.length;i++) {//点击次数最长的keyword
			if(total_3[i]>=max) {
				max = total_3[i];
				max_times_index = i;
				
			}
		}
		max = 0;
		int max_duration_index = 9999;
		for(i=0;i<total_2.length;i++) {//累计时间最多的keyword
			if(total_2[i]>=max) {
				max = total_2[i];
				max_duration_index = i;
				
			}
		}
		String ans = "";
		if(max_accumulate_habit>=6 && max_accumulate_habit<=12)ans+="你最喜欢在早上学习。";
		else if(max_accumulate_habit>=13 && max_accumulate_habit<=18)ans+="你最喜欢在下午学习。";
		else if(max_accumulate_habit>=19 && max_accumulate_habit<=23)ans+="你最喜欢在晚上学习。";
		else ans+="你喜欢在别人睡觉的时候学习。";
		
		String[] menu = get_keyword();
		String keyword1 = "";
		String keyword2 = "";
		if(max_duration_index < menu.length){
			keyword1 = menu[max_duration_index];
			ans+="你学习时间最长的标签是";
			ans+=keyword1;
			ans+=", 共学习了";
			ans+=String.valueOf(max);
			ans+="分钟。";
		}
			 
		if(max_times_index < menu.length) {
			keyword2 = menu[max_times_index];
			ans+="你点击次数最多的标签是";
			ans+=keyword2;
			ans+=". ";
		}
			
		
		return ans;
	}
	@Override
	public String socket_spyder(socket_res_obj a) {
		 String req_time_stamp;
		 String request_index;
		 String source_id;
		 int chapter_id;
		 String keyword="";
		 req_time_stamp=a.getMessage().getString("time_stamp");
		 request_index = a.getMessage().getString("request_index");
		switch(request_index) {
		 case "0":
			 JSONObject ans = get_menu(Long.parseLong(req_time_stamp));
			 ans.put("flag","0");
			 return(ans.toString());
			 //sessionasync.get(String.valueOf(flag)).sendText(ans.toString());

		 case "1":
			 String[] menu = get_keyword();
			 //System.out.print("\n爷的定时任务1执行拉\n"+Long.parseLong(req_time_stamp));
			 log.info(LogUtil.logSpec("Scheduled task 1 execution", 5));
			 chapter_id=Integer.parseInt(a.getMessage().getString("chapter_id"));
			 source_id=a.getMessage().getString("source_id");
			 keyword = menu[Integer.parseInt(source_id)];
			 //System.out.print("chapter_id: "+chapter_id);
			 if(chapter_id==1) {//菜鸟
				 JSONObject new_ans = get_runoob_comments(keyword);
				 JSONObject content = get_runoob_contents(keyword);
				 //System.out.print(new_ans);
				 new_ans.put("title",keyword);
				 new_ans.put("content",content);
				 new_ans.put("response_index",1);
				 new_ans.put("req_time_stamp",Long.parseLong(req_time_stamp));
				 new_ans.put("chapter_id",chapter_id);
				 new_ans.put("source_id",Integer.valueOf(source_id));
				 new_ans.put("abstract",content.getString("text").substring(0, 100)+"...");
				 new_ans.put("flag","0");
				 return(new_ans.toString());
				 //sessionasync.get(String.valueOf(flag)).sendText(new_ans.toString());
				 //flag=flag-1;
			 }
			 else {//CSDN
				 JSONObject csdn_ans = get_from_CSDN(chapter_id-2, keyword, Long.parseLong(req_time_stamp));
				 
				 csdn_ans.put("response_index",1);
				 csdn_ans.put("chapter_id",chapter_id);
				 csdn_ans.put("source_id",source_id);
				 csdn_ans.put("flag","0");
				 return(csdn_ans.toString());
				 //sessionasync.get(String.valueOf(flag)).sendText(csdn_ans.toString());
				 //flag=flag-1;
			 }
		 case "2":
			 int event_id;
			 event_id = Integer.valueOf(a.getMessage().getString("event_id"));
			 log.info(LogUtil.logSpec("Scheduled task 2 execution", 5));
			 String[] menu1 = get_keyword();
			 int lens = menu1.length;
			 
			 source_id=a.getMessage().getString("source_id");
			 keyword = menu1[Integer.parseInt(source_id)];
			 
			 JSONObject ans2 = new JSONObject();
			 ans2.put("response_index",2);
			 ans2.put("req_time_stamp",Long.parseLong(req_time_stamp));
			 JSONObject final_ans = new JSONObject();
			 String child_csdn = get_back_csdn(keyword);
			 if(child_csdn.equals("nope")||event_id != 4)  final_ans.put("flag","2");
			 else final_ans.put("flag","1");
			 final_ans.put("jbox",child_csdn);
			 final_ans.put("ans",ans2);
			 return final_ans.toJSONString();
		 }
		return null;	
	}
	@Override
	@Async()
	public void storage_userhabit(socket_res_obj res){
		// TODO Auto-generated method stub
		try {
			this.storage_userhabit(res.getMessage().getString("source_id"),Integer.valueOf(res.getMessage().getString("event_id")),messageHandler.Auto_getusername(res.getUserid()));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		log.info(LogUtil.logSpec("storage_userhabit asyn carry out once"));
	}
	@Autowired
	private AutoMessageHandler messageHandler;
	@Override
	public void get_feiyan_index() throws IOException {
		// TODO Auto-generated method stub
		//接口通信
        Feiyan a=new Feiyan();
        //获取结果
        LinkedList<HashMap<String,String>> res=a.get_info();
        //判断表中数据更新的时间与本次爬取的时间是否一致，如果一致则不需要添加，如果不一致，则执行添加操作
		List user = (List)jdbcTemplate.queryForList("SELECT `UpdateTime` FROM `t_feiyan`  GROUP BY `UpdateTime` ORDER BY UpdateTime DESC LIMIT 1");
		Timestamp date=(Timestamp) ((Map)user.get(0)).get("UpdateTime");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
         if(!simpleDateFormat.format(new Date(simpleDateFormat.parse(simpleDateFormat.format(date)).getTime()-28800000)).equals(res.get(0).get("UpdateTime"))){
          //更新数据库
        for(HashMap<String,String> item: res) {
        	jdbcTemplate.update("INSERT INTO `t_feiyan`(`UpdateTime`, `name`,`total_confirm`,`total_healRate`,`total_deadRate`,`today_confirm`,`today_isUpdated`,`province`,`zero`) VALUES (?,?,?,?,?,?,?,?,?)",item.get("UpdateTime"),item.get("name"),item.get("total_confirm"),item.get("total_healRate"),item.get("total_deadRate"),item.get("today_confirm"),item.get("today_isUpdated"),item.get("province"),item.get("zero"));	
               }
            }
     } catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
		}
	}
}


